<?php

namespace App\Modules\Apicrzs\Services\Public;

use App\Library\Junda\src\Consumption;
use App\Library\Pay\AliPayService;
use App\Library\Wechat\PayWeChatApi;
use App\Modules\Admin\Entities\Water\WaterOrderModel;
use function MongoDB\BSON\toCanonicalExtendedJSON;

class TestServices
{
    protected static $instance;
    /**
     * 实例化
     * @param array $config
     * @return static
     */
    public static function instance(array $config = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new static($config);
        }
        return self::$instance;
    }


    public function WeiXinByJundaByOrderSn($order_sn)
    {
        $order_info =  WaterOrderModel::getInstance()->getWaterOrderByOrderSn($order_sn);
        $pay_type = $order_info['pay_type'];
//        if ($pay_type=='weixin') {
//            $item_wx = PayWeChatApi::WXPayNotify($order_sn);
//        }elseif ($pay_type == 'alipay'){
//            $item_wx = AliPayService::instance()->query($order_sn);
//        }
        $res_junda = Consumption::GetPayConsumeByOrderNo($order_sn);

        if ($res_junda['success']){
            if ($res_junda['data']['IsConsumed']){
                $ConMoney = $res_junda['data']['ConMoney'];
                $conwater = $res_junda['data']['ConWater'];
                $amount = $order_info['amount']*100;
                if ($ConMoney == $amount) {
                    WaterOrderModel::getInstance()->updateWaterOrderByOrderSn($order_info['order_sn'], [
                        'con_water' => $conwater/1000,
                        'is_water' => 1,
                    ]);
                }
            }
        }
        exit(json_encode($res_junda['data']['IsConsumed']));

        return [
            'order_sn' => $order_info['order_sn'],
            'buy_money' => $order_info['buy_money'],
            'amount' => $order_info['amount'],
            'pay_type' => $order_info['pay_type'],
            'created_time' => $order_info['created_time'],
            'pay_item' => $item_wx ?? [],
            'junda_item' => $res_junda ?? [],
        ];
    }
}
